from math import sqrt

uplimit = 10**7
all_primes = [1]*(uplimit+1)

primes = []
index = 2
while index<=uplimit:
    if all_primes[index]:
        primes.append(index)
        k = 2
        while k * index <= uplimit:
            all_primes[k * index] = 0
            k += 1
    index += 1

limit = sqrt(uplimit)
min_n = 2
min_fain_n = 2.008
min_i = 2
min_j = 2
min_fain = 2
for i in primes:
    if i > limit:
        break
    for j in primes[::-1]:
        if i*j>uplimit:
            continue
        n = i * j
        fain = (i-1)*(j-1)
        if len(str(fain))==len(str(n)) and sorted(str(fain))==sorted(str(n)):
            if min_fain_n>n/fain:
                min_n = n
                min_fain_n = n/fain
                min_i = i
                min_j = j
                min_fain = fain
            print(n/fain)
print(min_n)
print(min_j)
print(min_i)
print(min_fain_n)
print(min_fain)